package com.song.controller.login;

import com.song.common.Result;
import com.song.entity.Constants;
import com.song.service.login.LoginService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LoginController {

    @RequestMapping("${api.version}/login/loginManage")
    public String loginManage(){
        return "login/login";
    }

    // 登录
    @RequestMapping("${api.version}/login")
    @ResponseBody
    public Result login(String account, String password) {
        Result result = new Result();
        UsernamePasswordToken token = new UsernamePasswordToken(account, password);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            result.setCode(Constants.SUCCESS_CODE);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(Constants.FAIL_CODE);
            result.setMsg("用户名或密码错误!");
        }
        return result;
    }

    // 未授权界面
    @RequestMapping("${api.version}/unauth")
    public String unauth(){
        return "error/unauth";
    }

    // 404 找不到
    @RequestMapping("${api.version}/error")
    public String error(){
        return "error/404";
    }
}
